package com.car.loan.project.tool.controller;

import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.cm.domain.CmBank;
import com.car.loan.project.cm.domain.CmBankScheme;
import com.car.loan.project.cm.domain.CmDealer;
import com.car.loan.project.cm.mapper.CmBankMapper;
import com.car.loan.project.cm.mapper.CmBankSchemeMapper;
import com.car.loan.project.cm.mapper.CmDealerMapper;
import com.car.loan.project.xr.util.repayment.FinancingAmountInterface;
import com.car.loan.project.xr.util.repayment.RepaymentFormulaInterface;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import java.math.BigDecimal;
import java.util.Map;
import javax.annotation.Resource;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @date 2020/2/15 17:51
 */
@RestController
@RequestMapping("/tool/calculator")
@Api(value = "计算器", tags = "工具接口-计算器")
public class CalculatorController {

    @Autowired
    private Map<String, RepaymentFormulaInterface> repaymentFormulaInterfaceMap;

    @Autowired
    private Map<String, FinancingAmountInterface> financingAmountInterfaceMap;

    @Resource
    private CmBankMapper cmBankMapper;

    @Resource
    private CmBankSchemeMapper cmBankSchemeMapper;

    @Resource
    private CmDealerMapper cmDealerMapper;

    @GetMapping("financingAmount")
    @ApiOperation(value = "获取融资金额or最低开票金额", response = CalculatorAmount.class)
    public AjaxResult getDealerList(
        @ApiParam(value = "银行按揭方案id", required = true) @RequestParam("bankSchemeId") Integer bankSchemeId,
        @ApiParam(value = "执行费率", required = true) @RequestParam("rate") BigDecimal rate,
        @ApiParam(value = "分期金额", required = true) @RequestParam("installmentAmount") BigDecimal installmentAmount,
        @ApiParam(value = "期数", required = true) @RequestParam("term") String term) {
        if (installmentAmount.setScale(0, BigDecimal.ROUND_HALF_UP)
            .compareTo(new BigDecimal("10000")) < 0) {
            return AjaxResult.error("分期金额不能少于1万");
        }
        CmBankScheme cmBankScheme = cmBankSchemeMapper.selectById(bankSchemeId);
        if (null == cmBankScheme) {
            return AjaxResult.error("无该银行对应方案");
        }
        String[] terms = cmBankScheme.getTerms().split(",");
        String[] rates = cmBankScheme.getRates().split(",");
        BigDecimal bankRate = null;
        for (int i = 0; i < terms.length; i++) {
            if (terms[i].equals(term)) {
                bankRate = new BigDecimal(rates[i]);
            }
        }
        if (null == bankRate) {
            return AjaxResult.error("该分期期数没有对应用户分期费率");
        }
        CmBank cmBank = cmBankMapper.selectById(cmBankScheme.getBankId());
        if (null == cmBank) {
            return AjaxResult.error("银行不存在");
        }
        if (StringUtils.isEmpty(cmBank.getRepaymentFormula())) {
            return AjaxResult.error("该银行还款公式未实现");
        }
        RepaymentFormulaInterface repaymentFormulaInterface = repaymentFormulaInterfaceMap
            .get(cmBank.getRepaymentFormula());

        Integer financingAmount = repaymentFormulaInterface
            .getFinancingAmount(installmentAmount, rate, bankRate);

        Integer miniBillingAmount = repaymentFormulaInterface
            .getMiniBillingAmount(financingAmount, installmentAmount, cmBankScheme.getCarType());

        CalculatorAmount calculatorAmount = new CalculatorAmount();
        calculatorAmount.setFinancingAmount(financingAmount);
        calculatorAmount.setMiniBillingAmount(miniBillingAmount);

        return AjaxResult.success(calculatorAmount);
    }


    @GetMapping("financingAmount2")
    @ApiOperation(value = "获取融资金额or最低开票金额", response = CalculatorAmount.class)
    public AjaxResult getDealerList(
        @ApiParam(value = "银行按揭方案id", required = true) @RequestParam("bankSchemeId") Integer bankSchemeId,
        @ApiParam(value = "执行费率", required = true) @RequestParam("rate") BigDecimal rate,
        @ApiParam(value = "分期金额", required = true) @RequestParam("installmentAmount") BigDecimal installmentAmount,
        @ApiParam(value = "期数", required = true) @RequestParam("term") String term,
        @ApiParam(value = "合作商户id", required = true) @RequestParam("dealerId") Long dealerId) {
        if (installmentAmount.setScale(0, BigDecimal.ROUND_HALF_UP)
            .compareTo(new BigDecimal("10000")) < 0) {
            return AjaxResult.error("分期金额不能少于1万");
        }

        CmDealer cmDealer = cmDealerMapper.selectById(dealerId);
        if (null == cmDealer) {
            return AjaxResult.error("无该经销商");
        }

        CmBankScheme cmBankScheme = cmBankSchemeMapper.selectById(bankSchemeId);
        if (null == cmBankScheme) {
            return AjaxResult.error("无该银行对应方案");
        }
        String[] terms = cmBankScheme.getTerms().split(",");
        String[] rates = cmBankScheme.getRates().split(",");
        BigDecimal bankRate = null;
        for (int i = 0; i < terms.length; i++) {
            if (terms[i].equals(term)) {
                bankRate = new BigDecimal(rates[i]);
            }
        }
        if (null == bankRate) {
            return AjaxResult.error("该分期期数没有对应用户分期费率");
        }
        CmBank cmBank = cmBankMapper.selectById(cmBankScheme.getBankId());
        if (null == cmBank) {
            return AjaxResult.error("银行不存在");
        }
        if (StringUtils.isEmpty(cmBank.getRepaymentFormula())) {
            return AjaxResult.error("该银行还款公式未实现");
        }
        RepaymentFormulaInterface repaymentFormulaInterface = repaymentFormulaInterfaceMap
            .get(cmBank.getRepaymentFormula());

        //TODO 待实现
//        cmDealer.get
        String key = "";
        FinancingAmountInterface financingAmountInterface = financingAmountInterfaceMap.get(key);
        if (null == financingAmountInterface) {
            return AjaxResult.error("该合作商户无对应公式");
        }
        Integer financingAmount = financingAmountInterface
            .getFinancingAmount(installmentAmount, rate, bankRate);
        Integer miniBillingAmount = repaymentFormulaInterface
            .getMiniBillingAmount(financingAmount, installmentAmount, cmBankScheme.getCarType());
        CalculatorAmount calculatorAmount = new CalculatorAmount();
        calculatorAmount.setFinancingAmount(financingAmount);
        calculatorAmount.setMiniBillingAmount(miniBillingAmount);
        return AjaxResult.success(calculatorAmount);
    }


    @Data
    private static class CalculatorAmount {

        @ApiModelProperty("融资金额")
        private Integer financingAmount;

        @ApiModelProperty("最低开票金额")
        private Integer miniBillingAmount;
    }


}
